﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;


public partial class _Default : System.Web.UI.Page 
{
    private static int error=0;
    int respid = 0;
    rUser user;
    DataSet usuEmployee = null,  usuClient = null, usuVendor = null, usuInv = null, usuGeneral = null, usuInactive= null;

    protected void Page_Load(object sender, EventArgs e)
    {
        tuser.Width = 180;
        tpass.Width = 180;
        cbperfil.Width = 180;
    }

    public void mostrarPaginas()
    {
        long permiso = 0;
        user = (rUser)(Session["userid"]);
        if (user != null)
        {
            permiso = long.Parse(user.getRestricted().ToString());
        }

        if ((permiso & 512) == 512) { Response.Redirect("admin//Workspace.aspx"); }
        
        if ((permiso & 65536) == 65536) { Response.Redirect("admin//OrderPlacement.aspx"); }
        if ((permiso & 131072) == 131072) { Response.Redirect("admin//Pro.aspx"); }
        if ((permiso & 262144) == 262144) {  Response.Redirect("sparkweb/SparkWeb.html"); }//Chat
        if ((permiso & 524288) == 524288) {  Response.Redirect("#"); }//Invoices
        if ((permiso & 1048576) == 1048576) {  Response.Redirect("#"); }//Admin
        if ((permiso & 2097152) == 2097152) { Response.Redirect("#"); }//Search
    }

    protected void lnk_signin_Click(object sender, EventArgs e)
    {
        string user = tuser.Text;
        string pass = tpass.Text;
        string datos = "";
        // string webser=Utility.setXml(user,Utility.cifrado(tpass.Text),1,1);

        int valperdil = int.Parse(cbperfil.SelectedValue);
        string webser = "";

        if (valperdil == 0)
        {
            datos = null;
            error = 1;
        }

        try
        {
            webser = Utility.setXml(user, tpass.Text, 1, valperdil);
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            datos = service.ValidaUsuario(webser);
        }
        catch (Exception s)
        {
            Utility.writeError(s.Message);
        }

        if (datos != null || datos != "")
        {
            string rxml1 = null, rxml3 = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            try
            {
                // Trae solo la lista de perfiles
                rxml1 = service.getPerfiles(1,1);
                DataSet perfiles = Utility.getPerfiles(rxml1);
                Session["Perfiles"] = perfiles;

                // Trae la lista completa de perfiles y permisos
                rxml3 = service.getPerfilesPermiso(1);
                DataSet perfilesPermiso = Utility.getPerfilesPermiso(rxml3);
                Session["PerfilesPermiso"] = perfilesPermiso;

                rxml3 = null;
                rxml3 = service.getPerfilesPermiso(0);
                Session["ProfilePermisos"] = Utility.getPerfilesPermiso(rxml3);

                // Utility.writeError("Error Default " + rxml1);
                usuGeneral = Utility.getTodosUsuarios(rxml1);
                usuEmployee = Utility.getUsuEmployee(rxml1);
                usuClient = Utility.getUsuClient(rxml1);
                usuVendor = Utility.getUsuVendor(rxml1);
                usuInv = Utility.getUsuInv(rxml1);

                rxml1 = service.getOpcionApp(1);
                DataSet permisos = Utility.getOpcionesbyApp(rxml1);
                Session["permisos"] = permisos;

                DataSet usuInac = DB.getUsuariosInactivos();
                Session["usuInac"] = usuInac;

                //usuarios inactivos
                string rxml2 = service.getUserbystatus(0);
                usuInactive = Utility.getUsuariosInactivos(rxml2);
            }
            catch (Exception ex) { Utility.writeError("Service para get perfiles" + ex.Message); }
            if (usuGeneral != null) { Session["Usuarios"] = usuGeneral; }
            if (usuEmployee != null) { Session["usuEmployee"] = usuEmployee; } 
            if (usuClient != null) { Session["usuClient"] = usuClient; }
            if (usuVendor != null) { Session["usuVendor"] = usuVendor; }
            if (usuInv != null) { Session["usuInv"] = usuInv; }
            if (usuInactive != null) { Session["usuInactive"] = usuInactive; }

            try
            {
                rUser ruserini = new rUser();
                rUser ruser = new rUser();
                ruserini = Utility.getRUser(datos);
                string rxml = service.getTodosDatosUsuario(ruserini.getUsuarioId());
                ruser = Utility.getRUser(rxml);
                if (ruser != null)
                {
                    if (ruser.getRespuestaId() == 1)
                    {
                        respid = 1;
                        foreach (RE_GenericBean obj in ruser.perfiles)
                        {
                            ruser.setPerfilId(obj.intC1.ToString());
                        }

                        Session["userid"] = ruser;
                         ArrayList activeusers = new ArrayList();
                        if (Application["ActiveUsers"] == null)
                        {
                           
                            activeusers.Add(ruser);
                            Application.Set("ActiveUsers",activeusers);
                        }
                        else {
                            activeusers = (ArrayList)Application["ActiveUsers"];
                            activeusers.Add(ruser);
                        }
                        
                    }
                    else
                    {
                        error = 2;
                    }
                }
                else
                {
                    error = 3;
                }
            }
            catch (Exception xmlexc)
            {
                Utility.writeError(xmlexc.StackTrace);
            }

            if (respid == 1)
            {
                Response.Redirect("admin//Workspace.aspx");
            }
        }
        if (error == 1)
        {// selectgroup
            Response.Write("<" + "script language='javascript'>");
            Response.Write("alert('Login denied. Please select group');");
            Response.Write("<" + "/script>");
            lbnotautenticate.Text = "Invalid Group";
        }
        if (error == 2)
        {
            Response.Write("<" + "script language='javascript'>");
            Response.Write("alert('Login denied. Please enter valid user, password or group');");
            Response.Write("<" + "/script>");
        }
        if (error == 3)
        {
            Response.Write("<" + "script language='javascript'>");
            Response.Write("alert('Login Fail');");
            Response.Write("<" + "/script>");
            Response.Redirect("Default.aspx");
        }

    }
}
